<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Bimbingan_model extends CI_Model {

	function __construct()
	{
		parent::__construct();
    }

    function getMahasiswa($nip)
	{
	    $query = "SELECT * FROM view_data_pa WHERE `NIP`='$nip'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	}

    function get($nip,$angkatan,$start,$limit)
	{
	    $query = "SELECT * FROM view_data_pa WHERE `NIP`='$nip' AND `NIM` LIKE '15_$angkatan%' ORDER BY NIM ASC LIMIT $start, $limit";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	}

    function countMahasiswa($nip,$angkatan)
	{
	    $query = "SELECT * FROM view_data_pa WHERE `NIP`='$nip' AND `NIM` LIKE '15_$angkatan%'";
        $query = $this->db->query($query);
        return $query->num_rows();
	}

    function getKkn($nip,$angkatan,$start,$limit)
	{
	    $query = "SELECT * FROM view_data_kkn WHERE `NIP_DOSEN_KKN`='$nip' AND `NIM` LIKE '15_$angkatan%' ORDER BY NIM ASC LIMIT $start, $limit";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	}

    function countMahasiswaKkn($nip,$angkatan)
	{
	    $query = "SELECT * FROM view_data_kkn WHERE `NIP_DOSEN_KKN`='$nip' AND `NIM` LIKE '15_$angkatan%'";
        $query = $this->db->query($query);
        return $query->num_rows();
	}

    function getPpl($nip,$angkatan,$start,$limit)
	{
	    $query = "SELECT * FROM view_data_ppl WHERE `NIP_DOSEN_PPL`='$nip' AND `NIM` LIKE '15_$angkatan%' ORDER BY NIM ASC LIMIT $start, $limit";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	}

    function countMahasiswaPpl($nip,$angkatan)
	{
	    $query = "SELECT * FROM view_data_ppl WHERE `NIP_DOSEN_PPL`='$nip' AND `NIM` LIKE '15_$angkatan%'";
        $query = $this->db->query($query);
        return $query->num_rows();
	}

	function getDetailMahasiswa($nip,$nim)
	{
	    $query = "SELECT * FROM view_data_pa WHERE `NIM`='$nim' AND `NIP`='$nip'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->row();
        }
        else $data = NULL;

        return $data;
	}

    function getDetailMahasiswaKknPplSkripsi($nim)
	{
	    $query = "SELECT * FROM view_data_pa WHERE `NIM`='$nim' LIMIT 1";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->row();
        }
        else $data = NULL;

        return $data;
	}

	function getDataMahasiswa($nim)
	{
	    $query = "SELECT `KODE_PRODI` FROM view_data_mahasiswa WHERE `NIM`='$nim' LIMIT 1";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->row();
        }
        else $data = NULL;

        return $data;
	}

	function getMahasiswaKkn($nip)
	{
	    $query = "SELECT * FROM view_data_kkn WHERE `NIP_DOSEN_KKN`='$nip'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	}
	
	
	function getDetailMahasiswaKkn($nip,$nim)
	{
	    $query = "SELECT * FROM view_data_kkn WHERE `NIM`='$nim' AND `NIP_DOSEN_KKN`='$nip'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->row();
        }
        else $data = NULL;

        return $data;
	}
	
	function getMahasiswaPpl($nip)
	{
	    $query = "SELECT * FROM view_data_kkn WHERE `NIP_DOSEN_PPL`='$nip'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	}
	
	
	function getDetailMahasiswaPpl($nip,$nim)
	{
	    $query = "SELECT * FROM view_data_kkn WHERE `NIM`='$nim' AND `NIP_DOSEN_PPL`='$nip'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->row();
        }
        else $data = NULL;

        return $data;
	}
	
	function getMahasiswaSkripsi($nip)
	{
	    $query = "SELECT * FROM view_data_skripsi WHERE `NIP`='$nip'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	}
	
	
	function getDetailMahasiswaSkripsi($nip,$nim)
	{
	    $query = "SELECT * FROM view_data_skripsi WHERE `NIM`='$nim' AND `NIP`='$nip'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->row();
        }
        else $data = NULL;

        return $data;
	}

    function getViewKrsTemp($nim)
	  {
	    $query = "SELECT * FROM view_krs_temp where `NIM_MAHASISWA`='$nim'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	  }
	  
	  function getViewKhsAll($nim,$tahun,$kode_semester)
	  {
	    $query = "SELECT * FROM view_khs_all WHERE `NIM`='$nim' and `TAHUN_AJARAN`='$tahun' and `ID_SEMESTER`='$kode_semester'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	  }

      function getViewKhsAllByNim($nim)
	  {
	    $query = "SELECT * FROM view_khs_all where `NIM`='$nim'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	  }
	  
	  function insertDataValidasi($nim, $mk_tersedia)
	  {
		$query = "INSERT INTO enrollments (MK_TERSEDIA_ID,NIM_MAHASISWA) 
            VALUES ('{$mk_tersedia}','{$nim}')";
        $query = $this->db->query($query);
	  }
	  
	  function updateDataValidasi($nim)
      {
        $query = "UPDATE temporary_enrollments SET `APPROVED`=1 WHERE NIM_MAHASISWA='$nim'";
        $query = $this->db->query($query);
      }
	  
	  function getJadwalKuliah($kode_prodi,$kode_semester,$kode_tahun,$id_hari)
	{

	    $query = "SELECT * FROM view_jadwal_kuliah WHERE `KODE_PRODI`='$kode_prodi' AND `SEMESTER`='$kode_semester' AND `TAHUN`='$kode_tahun' AND `ID_HARI`='$id_hari' ORDER BY `ID_WAKTU_KULIAH` ASC";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	}

    function getJadwalUjian($kode_prodi,$kode_semester,$kode_tahun)
	{

	    $query = "SELECT * FROM view_jadwal_ujian WHERE `KODE_PRODI`='$kode_prodi' AND `SEMESTER`='$kode_semester' AND `TAHUN`='$kode_tahun' ORDER BY `TANGGAL_UJIAN`,`MULAI` ASC";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	 }

     function getDaftarMkPresensi($kode_semester,$kode_tahun,$nim)
	 {
	    $query = "SELECT * FROM view_presensi WHERE `KODE_SEMESTER`='$kode_semester' AND `TAHUN_AJARAN`='$kode_tahun' AND `NIM`='$nim'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	  }

      function getPresensiData($tahun,$kode_semester,$kode_mk)
	  {
	    $query = "SELECT * FROM view_presensi where `TAHUN_AJARAN`='$tahun' and `KODE_SEMESTER`='$kode_semester' and `KODE_MK`='$kode_mk'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	  }

      function getPresensiDataNim($nim,$tahun,$kode_semester,$kode_mk)
	  {
	    $query = "SELECT * FROM view_presensi where `NIM`='$nim' and `TAHUN_AJARAN`='$tahun' and `KODE_SEMESTER`='$kode_semester' and `KODE_MK`='$kode_mk'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->row();
        }
        else $data = NULL;

        return $data;
	  }

      function getViewKrsAll($nim,$tahun,$kode_semester)
	  {
	    $query = "SELECT * FROM view_krs_all WHERE `NIM`='$nim' and `TAHUN_AJARAN`='$tahun' and `ID_SEMESTER`='$kode_semester'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	  }

      

      function dataMatakuliahTersediaById($kode_mkb)
	  {
	    $query = "SELECT * FROM view_mk_tersedia where `ID_TERSEDIA`='$kode_mkb'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->row();
        }
        else $data = NULL;

        return $data;
	  }

      function dataJamKuliah($id_mk_tersedia)
	  {

	    $query = "SELECT ID_HARI, MULAI, SELESAI FROM view_jadwal_kuliah WHERE `ID_MK_TERSEDIA`='$id_mk_tersedia'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->row();
        }

        return $data;
	  }

      function getViewJadwalKuliah($kode_tahun, $kode_semester, $kode_prodi)
	  {
	    $query = "SELECT * FROM view_jadwal_kuliah WHERE TAHUN = '$kode_tahun' AND SEMESTER = '$kode_semester' AND KODE_PRODI='$kode_prodi' ORDER BY ID_HARI,MULAI";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	  }

      function jumlahPeminatMk($id_mk_tersedia)
	  {
	    $query = "SELECT * FROM temporary_enrollments WHERE ID_MK_TERSEDIA = '$id_mk_tersedia'";
        $query = $this->db->query($query);

        return $query->num_rows();
	  }

      function viewMkTersedia_cekPrasyarat($kode_prasyarat1, $kode_prasyarat2)
      {
        $query = "SELECT `ID_TERSEDIA` FROM view_mk_tersedia WHERE NO_MK= '$kode_prasyarat1' OR NO_MK = '$kode_prasyarat2'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
      }

      function viewDataEnrollment($id_tersedia,$nim)
      {
        $query = "SELECT * FROM view_data_enrollment WHERE ID_MK_TERSEDIA = '$id_tersedia' AND NIM = '$nim'";
        $query = $this->db->query($query);

        return $query->num_rows();
      }

      function getKodeMkByNoMk($no_mk=NULL)
      {
        $query = "SELECT * FROM matakuliah WHERE NO = '$no_mk'";
        $query = $this->db->query($query);

        return $query->row();
      }

      function insertDataKrs($tersedia, $nim)
      {
        $query = "INSERT INTO temporary_enrollments VALUES ('{$tersedia}','{$nim}','')";
        $query = $this->db->query($query);
      }

      function dataDeleteKrs($nim, $id_mk_tersedia)
      {
        $query = "DELETE FROM temporary_enrollments WHERE `NIM_MAHASISWA`='$nim' AND `ID_MK_TERSEDIA`='$id_mk_tersedia' AND `APPROVED` <> 1";
        $query = $this->db->query($query);
      }

      function cekValidasi($nim,$kode_semester,$kode_tahun)
      {
        $query = "SELECT * FROM temporary_enrollments where `NIM_MAHASISWA`='$nim' AND APPROVED = 1";
        $query = $this->db->query($query);

        if($query->num_rows())
          $data = $query->row();
        else
          $data = NULL;

        return $data;
      }

      function getNeraca($nim)
      {
        $query = "SELECT * FROM view_data_neraca WHERE `NIM`='$nim' ORDER BY `NAMA_TAHUN`";
        $query = $this->db->query($query);

        if($query->num_rows())
          $data = $query->row();
        else
          $data = NULL;

        return $data;
      }
}

